package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.events.commonEvent.ClearTitleWindowEvent;
	import com.tongji.housediy.events.UserLoadandRegisterEvent.LoginEvent;
	import com.tongji.housediy.events.commonEvent.HideTitleWinEvent;
	import com.tongji.housediy.events.commonEvent.ListItemMouseEvent;
	import com.tongji.housediy.events.goodsEvent.CollectionAdminEvent;
	import com.tongji.housediy.model.CollectionModel;
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.utils.PopUpAdmin;
	import com.tongji.housediy.view.renderer.GoodsItemRenterer;
	import com.tongji.housediy.view.renderer.GoodsItemRentererMediator;
	import com.tongji.housediy.vo.Collection;
	import com.tongji.housediy.vo.Goods;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/7/
	 */ 
	public class AddCollectionMediator extends Mediator
	{
		[Inject]
		public var addView:AddCollectionTitleWindow;
		
		[Inject]
		public var model:GoodsModel;
		
		[Inject]
		public var userModel:UserInforModel;
		
		[Inject]
		public var collection:CollectionModel;
		public var collectionInstance:Collection;
		private var goods:Goods;
		
		public function AddCollectionMediator()
		{
			super();
		}
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(addView.goodsName, "text", model, "goodsName");
			BindingUtils.bindProperty(addView.brandName, "text", model, "goodsBrand");
 
			//this.addContextListener(HideTitleWinEvent.HIDE_ADD_COLLECTION, closeTitleWinHandler, HideTitleWinEvent);
			this.addContextListener(LoginEvent.LOGIN_SUCCESS, loginSuccessHandler, LoginEvent);
			//addView.addEventListener(MouseEvent.ROLL_OUT, itemMouseOutHandler);
			addView.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			addView.addCollection.addEventListener(MouseEvent.CLICK, addColletionHandler);
			addView.infoBtn.addEventListener(MouseEvent.CLICK, inforBtnClickHandler);
			goods = (addView.owner as GoodsItemRenterer).data as Goods;
			//this.addContextListener(ClearTitleWindowEvent.SET_EXIST_WINDOW_FALSE, closeTitleWinHandler, ClearTitleWindowEvent);
		}
		
		private function inforBtnClickHandler(event:MouseEvent):void
		{
			(addView.owner as GoodsItemRenterer).dispatchEvent(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_UP));
			//this.dispatch(new ShowGoodsInforEvent(ShowGoodsInforEvent.SHOW_GOODS_INFOR));
			PopUpManager.removePopUp(addView);
			mediatorMap.removeMediatorByView(addView);
		}
		
		private function mouseOutHandler(event:MouseEvent):void
		{	
			if(event.localX < addView.width && event.localY < addView.height && event.localX > 0 && event.localY != -1)
			{}
			else
			{
				if(event.localY == -1 && event.localX > 45 && event.localX < 119)
				{}
				else
				{
					PopUpManager.removePopUp(addView);
					mediatorMap.removeMediatorByView(addView);
					dispatch(new ClearTitleWindowEvent(ClearTitleWindowEvent.SET_EXIST_WINDOW_FALSE));
				}
			}
		}
		
		private function itemMouseOutHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(addView);
			mediatorMap.removeMediatorByView(addView);
		}
		
		/*private function closeTitleWinHandler(event:ClearTitleWindowEvent):void
		{
			PopUpManager.removePopUp(addView);
			mediatorMap.removeMediatorByView(addView);
		}*/
		
		private function addColletionHandler(event:MouseEvent):void
		{
			//collection.goodsArray.addItem(goods.goodsID);
			if(!userModel.userID)
			{
				PopUpAdmin.popupCenterWindow("userLogin",addView,mediatorMap);
			}
			else
			{
				collectionInstance = new Collection();
				collectionInstance.typeID = 2;
				//collectionInstance.typeName = model.goodsType;
				collectionInstance.userID = Number(userModel.userID);
				collectionInstance.goodsIDS = "6";
				dispatch(new CollectionAdminEvent(CollectionAdminEvent.ADD_COLLECTIONS, collectionInstance));
			}
			PopUpManager.removePopUp(addView);
			mediatorMap.removeMediatorByView(addView);
		}
		
		private function loginSuccessHandler(event:LoginEvent):void
		{
			dispatch(new CollectionAdminEvent(CollectionAdminEvent.ADD_COLLECTIONS, collectionInstance));	
		}
		
		private function infoBtnHandler(event:MouseEvent):void
		{
			addView.dispatchEvent(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_UP));
		}
	}
}